package com.xtedu.examproject.service;

import com.xtedu.examproject.dao.base.UploadedFileDao;
import com.xtedu.examproject.dao.impl.UploadedFileDaoImpl;
import com.xtedu.examproject.entity.UploadedFile;
import com.xtedu.examproject.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/*
 * Author: xzy
 * Date: 2025/10/15
 * Last Modified by: xzy
 * Last Modified date: 2025/10/15
 * Description: TODO - Add file description here
 */
public class UploadedFileService {
    private UploadedFileDao uploadedFileDao;

    public UploadedFileService() {
        uploadedFileDao = new UploadedFileDaoImpl();
    }

    public int insert(UploadedFile uploadedFile) throws Exception {
        return uploadedFileDao.insert(uploadedFile);
    }

    /*
     * 通过考试ID查询上传文件url
     * */
    public static List<UploadedFile> findUrlByExamId(int examId) throws Exception {
        String sql = "select upload_path,original_name from uploaded_files where exam_id = ?";
        List<UploadedFile> UploadedFiles = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, examId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                UploadedFile uploadedFile =new UploadedFile();
                uploadedFile.setExamId(examId);
                uploadedFile.setUploadPath(rs.getString("upload_path"));
                uploadedFile.setOriginalName(rs.getString("original_name"));
                UploadedFiles.add(uploadedFile);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return UploadedFiles;
    }

}
